Embedding Guide
One of the best features about Console.js is the ability to embed a console on any website.
We have tried to make this as simple as possible.
To start off you need to remember to include the following files:
Example using publicly hosted files...
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.9/ace.js"></script>
<script src="https://cdn.rawgit.com/TarVK/chromeConsole/version-1.0/console.js"></script>
<link rel="stylesheet" href="https://cdn.rawgit.com/TarVK/chromeConsole/version-1.0/console.css" type="text/css" />
Example using locally hosted files...
<script src="/libs/jQuery/jquery.js"></script>
<script src="/libs/ace/ace.js"></script>
<script src="/libs/console/console.js"></script>
<script src="/libs/console/console.css"></script>
After adding the above files, we can change an existing html element into
a console. You do this as follows:
var cons = $(".console").console({
onInput: function(text){
this.output(text);
},
});
For a full list of options please see the
API Reference.
Basic console template
If you just want to copy and paste some code to get going, here is some example code:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.9/ace.js"></script>
<script src="https://cdn.rawgit.com/TarVK/chromeConsole/version-1.0/console.js"></script>
<link rel="stylesheet" href="https://cdn.rawgit.com/TarVK/chromeConsole/version-1.0/console.css" type="text/css" />
<style>
html, body, .console{
width: 100%;
height: 100%;
margin: 0px;
}
</style>
<script>
$(function(){
var cons = $(".console").console({
onInput: function(text){
this.output(text);
},
});
});
</script>
</head>
<body>
<div class="console"></div>
</body>
</html>
Setting up the JavaScript console
To setup the JavaScript console you will need
and setup the worker correctly.
Consider the following examples:
Example using public hosted files...
<script src="https://cdn.rawgit.com/TarVK/chromeConsole/version-1.0/extensions/jsConsole/jsConsole.sf.js"></script>
<script>
var workerURL = "https://cdn.rawgit.com/TarVK/chromeConsole/version-1.0/extensions/jsConsole/jsConsole.sf.js";
$(function(){
var cons = $(".console").jsConsole({worker:getWorker()});
cons.info("Type javascript here!");
});
function getWorker(){
var req = new XMLHttpRequest();
req.open("GET", workerURL, false);
req.send();
var blob;
try{
blob = new Blob([req.responseText], {type: 'application/javascript'});
}catch(e){
window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
blob = new BlobBuilder();
blob.append(req.responseText);
blob = blob.getBlob();
}
return URL.createObjectURL(blob);
}
</script>
Example using locally hosted files...
<script src="/libs/console/extensions/jsConsole/jsConsole.sf.js"></script>
<script>
var cons = $(".console").workerJsConsole({worker:"/libs/console/extensions/jsConsole/jsConsole.sf.js"});
</script>
Full JavaScript console example
If you just want to copy and paste some code to get going, here is some example code
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.9/ace.js"></script>
<script src="https://cdn.rawgit.com/TarVK/chromeConsole/version-1.0/console.js"></script>
<script src="https://cdn.rawgit.com/TarVK/chromeConsole/version-1.0/extensions/jsConsole/jsConsole.sf.js"></script>
<link rel="stylesheet" href="https://cdn.rawgit.com/TarVK/chromeConsole/version-1.0/console.css" type="text/css" />
<style>
html, body, .console{
width: 100%;
height: 100%;
margin: 0px;
}
</style>
<script>
var workerURL = "https://cdn.rawgit.com/TarVK/chromeConsole/version-1.0/extensions/jsConsole/jsConsole.sf.js";
$(function(){
var cons = $(".console").jsConsole({worker:getWorker()});
cons.info("Type javascript here!");
});
function getWorker(){
var req = new XMLHttpRequest();
req.open("GET", workerURL, false);
req.send();
var blob;
try{
blob = new Blob([req.responseText], {type: 'application/javascript'});
}catch(e){
window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
blob = new BlobBuilder();
blob.append(req.responseText);
blob = blob.getBlob();
}
return URL.createObjectURL(blob);
}
</script>
</head>
<div class=console></div>
</html>
JavaScript External Console Window
If you don't have access to the console, because, for example, the built in console in the browser is blocked,
this plugin can be used to create a JavaScript console to interact with your code for debugging needs.
Unfortunately windows can only be opened on user interaction,
so the console will only open after you click somewhere in the page that you are trying to add the console to.
You can either embed the code on your page, or inject the console into a random page.
Embedding the external console window onto your own page
<!DOCTYPE html>
<html>
<head>
<script>
window.script = document.createElement("script");
script.setAttribute("src", "https://cdn.rawgit.com/TarVK/chromeConsole/version-1.0/extensions/jsConsolePlugin/jsConsolePlugin.js");
document.head.appendChild(script);
</script>
<style>
.someRandomPageContents{
background-color: rgb(200,200,200);
}
</style>
<script>
var b = "some variable accessable through the embedded console";
setInterval(function(){
console.log("some message");
}, 2000);
</script>
</head>
<div class="someRandomPageContents">
Click anywhere on the page to open the javascript console
</div>
</html>
Injecting the external console window into an arbitrary page
Unfortunately many web pages like google and youtube don't allow scripts of random domains to be ran, so the injection won't work for these pages.
example.com is an example of a page that this will work on correctly.
There are 3 steps to injecting the console onto an page:
1. Goto your webpage and type 'javascript:' in the addressbar
2. Copy the lines of code provided below and paste them in the addressbar
3. Hit enter and click somewhere in the webpage
Your console should now appear.
window.script = document.createElement("script");
script.setAttribute("src", "https://cdn.rawgit.com/TarVK/chromeConsole/version-1.0/extensions/jsConsolePlugin/jsConsolePlugin.js");
document.head.appendChild(script);
Page has been last updated on: 27 Jan 2018